import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.text.DateFormat;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

//import Professor.MyTableModel;

public class Admin{

	public  	JButton     CreateAccount      				= new JButton("Activate Accounts"); 
	public		JButton     AsignCourse  	   				= new JButton("Assign Courses"); 
	public		JButton     Addcourse  	    				= new JButton("Add Course"); 
	public		JButton     ShowLog       					= new JButton("View Log"); 
	public		JLabel      Headermsg 	  					= new JLabel("Welcome to Grade Management System-server");
	public		static 		JTextField AdminCentermsg 		= new JTextField("",40);
	public		JPanel      Maininterface     				= new JPanel();
	public 		JButton     AddnewAccount 							= new JButton("Create"); 
	public 		static		JTextField Username 			= new JTextField("");
	public 		static		JTextField UserPass 			= new JTextField("");
	public  	static		JTextField Useremail			= new JTextField("");
	public 		static		JTextField Userfullname 		= new JTextField("");
	public  	static		JTextField Useraddress 			= new JTextField("");
	public  	static 		JTextField Userphone 			= new JTextField("");
	public 		static		String 	   id;
	public 		static 		JTextField CourseNumber 		= new JTextField("");
	public 		static 		JTextField CourseName 			= new JTextField("");
	public 		static 		JTextField CourseHour			= new JTextField("");
	public 		static 		JTextField CourseInstrcuter 	= new JTextField("");
	public 		static 		JButton    AddcourseToDB 		= new JButton("Add Course");
    public 		static 		JButton    Adminassign			= new JButton("Submit");
	public 		static 		JComboBox  SemList				 = new JComboBox();
	public 		static		JComboBox  Semlistwithevents	 = new JComboBox();
	public 		static  	JComboBox  courselist 			 = new JComboBox();
	public		static 	 	JTextField assignedAccountID     = new JTextField("");
	public 		static 		JComboBox  Role				     = new JComboBox();
	public 		static 		JButton submitbtn 				= new JButton("Approve");
	public 		static 		JButton searchbtn 				= new JButton("Search");
	public 		static 		JTextField searchtxt 				= new JTextField("");
	public 		static 		JLabel searchlbl 				= new JLabel("Search for a particular username: ");
	public 		static 		JLabel statuslbl 				= new JLabel("Sorry: There are no recoreds found.. ");
	private JTable table;
	public JTextField usernametxt = new JTextField();
	private JLabel usernamelbl;
	public Connection connection;
	public static String[][] CurrentStudentList;
	 			static      String[] Sem = { "FA2011", "SP2011", "SM2011","FA2012", "SP2012", "SM2012","FA2013", "SP2013", "SM2013"};
	public static JPanel J ;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTextArea ActionTextArea;
	 int NumberOfRows,NumberOfColumns;
	 Object[][] MyArray=null;
	 String[][] adminlog=null;
	 String[][] adminacc=null;
	public static JTable DataBaseTable = new JTable();
	public static JTable DataBaseTablee = new JTable();
	public static String clientip;
	Admin()
	{
		
	}
	 
	public JPanel Interfase ()
	{
		// the Menue in the admin interface
		 JPanel MainAdmininterface		 = new JPanel();
		 JPanel MainAdmininterfaceCenter = new JPanel();
		 MainAdmininterface.setLayout(new GridLayout(8,1));
		 MainAdmininterface.add(CreateAccount);
		 MainAdmininterface.add(AsignCourse);
		 MainAdmininterface.add(Addcourse);
		 MainAdmininterface.add(ShowLog);
		 Maininterface.add(MainAdmininterface,BorderLayout.WEST);
		 return( MainAdmininterface );
	}
	
	
	public JPanel InterfaceNorth(){
		//the Header
		JPanel interfaceNorth=new JPanel();
		JLabel l1=new JLabel(" Welcome Admin ");
		interfaceNorth.add(l1);
		return(interfaceNorth);
	}
	public JPanel SouthPanel(String c){
		//the Header
		JPanel interfaceSouth=new JPanel();
		AdminCentermsg.setFocusable(false);
		AdminCentermsg.setText(c);
		interfaceSouth.add(AdminCentermsg);
		return(interfaceSouth);
	}
	public JPanel Addcourse(){
			 JPanel AddCourses=new JPanel();
			 AddCourses.setLayout(new GridLayout(8,7));
		     SemList = new JComboBox(Sem);
			 JLabel Semesterlb		 = new JLabel("Semester ");
			 JLabel Coursenlb 		 = new JLabel("Course Number ");
			 JLabel Coursenamelb	 = new JLabel("Course Name ");
			 JLabel CourseHourlb	 = new JLabel("Course Hour ");
			 JLabel CourseInstrlb	 = new JLabel("Course Instrcuter ");
			 AddCourses.add(Semesterlb);
			 AddCourses.add(SemList);
			 AddCourses.add(Coursenlb);
			 AddCourses.add(CourseNumber);
			 AddCourses.add(Coursenamelb);
			 AddCourses.add(CourseName);
			 AddCourses.add(CourseHourlb);
			 AddCourses.add(CourseHour);
			 AddCourses.add(CourseInstrlb);
			 AddCourses.add(CourseInstrcuter);
			 AddCourses.add(AddcourseToDB);
			 return(AddCourses);
	}

	
	
	 public JPanel AssignRole(String CN ){
		 JPanel Assignt=new JPanel();
		 Assignt.setLayout(new GridLayout(5,2));
		 JLabel CourseN = new JLabel("Course Number");
		 String[] res=CN.split(";");
		 courselist= new JComboBox(res);
	     JLabel AccountIDlb = new JLabel("User Name");
	     JLabel semester = new JLabel("Semester");
	     Semlistwithevents = new JComboBox(Sem);
	     Assignt.add(semester);
	     Assignt.add(Semlistwithevents);
	     
		 if(CN.isEmpty())
		 {
			 Assignt.setLayout(new GridLayout(4,2));
			 JLabel Tasklb = new JLabel("There are no courses for this semester.");
			 Assignt.add(new JLabel(""));
			 Assignt.add(Tasklb);
			 Assignt.add(new JLabel(""));
			 Assignt.add(new JLabel(""));
		 }
		 else
		 {
			 JLabel Tasklb = new JLabel("Role");
		     String[] Typeoftask ={ "Professor", "Student","Ta"};
		     Role = new JComboBox(Typeoftask);
		     JLabel space22 = new JLabel("");
		     Assignt.add(CourseN);
		     Assignt.add(courselist);
		     Assignt.add(AccountIDlb);
		     Assignt.add(assignedAccountID);
		     Assignt.add(Tasklb);
		     Assignt.add(Role);
		     Assignt.add(space22);
		     Assignt.add(Adminassign);
		 }
	     return (Assignt);
	 }
	 public JPanel logtabel(String [][] table) 
		{	
		 	
		 	J=new JPanel( );
			
		
			//String[] StudentList= line.split(";");
			String [] AttributeNames;
			String state;
			
			javax.swing.JScrollPane jScrollPane1;
			NumberOfRows=table.length;
			NumberOfColumns=2;
			JLabel label;
			
			MyArray=new Object[NumberOfRows][NumberOfColumns];
			adminlog=new String[NumberOfRows][8];

			int l=1;
			
					for(int R=0;R<table.length;R++)
					{
								for(int i=0;i<8;i++)
								{
									adminlog[R][i] = table[R][i];
								}
					}
			for(int R=0;R<table.length;R++)
				{
				MyArray[R][0] = adminlog[R][7];
				MyArray[R][1] = adminlog[R][5];
				}
			
			J.add(jScrollPane1 = new javax.swing.JScrollPane());
			J.add(DataBaseTable);

			J.setLayout(null);
			

			
			DataBaseTable.setModel(new javax.swing.table.DefaultTableModel(
			new Object [][] { {null},{null},{null},{null}},new String [] { ""}) 
			
			{
			Class[] types = new Class [] { java.lang.Object.class};
			boolean[]canEdit=new boolean[]{false};
			
			public Class getColumnClass(int columnIndex) {
			return types [columnIndex];
			}
			
			public boolean isCellEditable(int rowIndex, int columnIndex) {
			return canEdit [columnIndex];
			}
			});
			
			DataBaseTable.setModel(new MyTableModel());
			DataBaseTable.setVisible(true);
				
			jScrollPane1.setViewportView(DataBaseTable);
			
			J.add(jScrollPane1);
			jScrollPane1.setBounds(3, 120, 550, 150);
			
			J.revalidate();
			return(J);
			}	
		
			public void DataBaseTableMouseReleased() {
//			usernametxt.setText(new MyTableModel().getValueAt(DataBaseTable.getSelectedRow(),0).toString());
//			statuslbl.setText("");
//			statuslbl.setBackground(null);
			}
			public JPanel see () {
				JPanel j=new JPanel();
				j.setLayout(new GridLayout(0,2));
				JLabel ActivityType=new JLabel("Activity Type:");
				j.add(ActivityType);
				JLabel ActivityTypelb=new JLabel(adminlog[DataBaseTable.getSelectedRow()][4]);
				j.add(ActivityTypelb);
				JLabel ActivityTime=new JLabel("ActivityTime:");
				j.add(ActivityTime);
				JLabel ActivityTimelb=new JLabel(adminlog[DataBaseTable.getSelectedRow()][1]);
				j.add(ActivityTimelb);
				
				JLabel SubjectName=new JLabel("Admin Name:");
				j.add(SubjectName);
				JLabel SubjectNamelb=new JLabel(adminlog[DataBaseTable.getSelectedRow()][3]);
				j.add(SubjectNamelb);
				
				JLabel ActivityPriorty=new JLabel("Activity Priorty:");
				j.add(ActivityPriorty);
				JLabel ActivityPriortylb=new JLabel(adminlog[DataBaseTable.getSelectedRow()][5]);
				j.add(ActivityPriortylb);
				
				JLabel ActivityValue=new JLabel("Activity Value:");
				j.add(ActivityValue);
				JTextArea ActivityValuelb=new JTextArea(adminlog[DataBaseTable.getSelectedRow()][6]);
				jScrollPane2 = new javax.swing.JScrollPane(ActivityValuelb);
				ActivityValuelb.setEditable(false);
				j.add(jScrollPane2);
				JLabel ActivityDesc=new JLabel("Activity Description:");
				j.add(ActivityDesc);
				JLabel ActivityDesclb=new JLabel(adminlog[DataBaseTable.getSelectedRow()][7]);
				j.add(ActivityDesclb);
				
				
				
//				JLabel ActivityType=new JLabel("Activity Type:");
//				j.add(ActivityType);
//				JLabel email=new JLabel(adminlog[DataBaseTable.getSelectedRow()][0]);
//				j.add(email);
//				JLabel e2=new JLabel(adminlog[DataBaseTable.getSelectedRow()][1]);
//				j.add(e2);
//				JLabel e3=new JLabel(adminlog[DataBaseTable.getSelectedRow()][2]);
//				j.add(e3);
//				JLabel e4=new JLabel(adminlog[DataBaseTable.getSelectedRow()][3]);
//				j.add(e4);
				return(j);
				
				
				}

			class MyTableModel extends AbstractTableModel {
			//You can choose any long value for serialVersionUID.
			//I chose 317 because this is CSCI 317.
			String[] s={"ActivityType","ActivityPriorty"};
			private static final long serialVersionUID=317L;
			public int getColumnCount() {
			return (NumberOfColumns);
			}
			public int getRowCount() {
			return(NumberOfRows);
			}
			public String getColumnName(int i) {
			return (s[i]);
			}
			public java.lang.Object getValueAt(int row, int column) {
			return(MyArray[row][column]);
			}
			public Class getColumnClass(int c) {
			return getValueAt(0, c).getClass();
			}
			public boolean isCellEditable(int row, int col) {
			if (col < 7) { 
			return false;
			} else {
			return true;
			}
			}
			public void setValueAt(Object value, int row, int col) {
			MyArray[row][col] = value;
			fireTableCellUpdated(row, col);
			}        
			}
			
			// ############################################## Activate Accounts ############################################## 
			
			public JPanel GetAccounts(String [][] table) 
			{	
				J=new JPanel( );
				J.add(searchlbl);
				searchlbl.setBounds(10,40,300,30);
				
				J.add(searchtxt);
				searchtxt.setBounds(10,70,200,30);
				
				J.add(searchbtn);
				searchbtn.setBounds(260,65,100,40);
				
				
				
				J.setLayout(null);
				J.revalidate();
				
			 	if (table.length<1){
			 		;
			 		J.add(statuslbl);
					statuslbl.setOpaque(true);
			 	    statuslbl.setBackground(Color.RED);
			 	    statuslbl.setForeground(Color.WHITE);
					statuslbl.setBounds(12,190,300,40);
			 	}else
			 	{
			 	
			 	DataBaseTablee = new JTable();
				//String[] StudentList= line.split(";");
				String [] AttributeNames;
				String state;
				
				javax.swing.JScrollPane jScrollPane1;
				NumberOfRows=table.length;
				NumberOfColumns=6;
				JLabel label;
				System.out.println(table.length);
				MyArray=new Object[NumberOfRows][NumberOfColumns];
				adminacc=new String[NumberOfRows][6];
				int l=1;
				
						for(int R=0;R<table.length;R++)
						{
									for(int i=0;i<6;i++)
									{
										adminacc[R][i] = table[R][i];
									}
						}
				for(int R=0;R<table.length;R++)
					{
					MyArray[R][0] = adminacc[R][0];
					MyArray[R][1] = adminacc[R][1];
					MyArray[R][2] = adminacc[R][2];
					MyArray[R][3] = adminacc[R][3];
					MyArray[R][4] = adminacc[R][4];
					MyArray[R][5] = adminacc[R][5];
					//MyArray[R][6] = adminacc[R][6];
					//MyArray[R][7] = adminacc[R][7];
					}
				//JPanel s = new JPanel();
				
				J.add(searchlbl);
				searchlbl.setBounds(10,40,300,30);
				
				J.add(searchtxt);
				searchtxt.setBounds(10,70,200,30);
				
				J.add(searchbtn);
				searchbtn.setBounds(260,65,100,40);
				
				//J.add(s);
				J.add(jScrollPane1 = new javax.swing.JScrollPane());
				J.add(DataBaseTablee);

				
				J.setBorder(BorderFactory.createTitledBorder("Activate New Users Accounts"));
				
				J.add(usernamelbl = new JLabel());
				J.add(usernametxt);

				J.setLayout(null);
				

				J.add(usernamelbl);
				usernamelbl.setText("Student name");
				usernamelbl.setBounds(10,280,200,30);
				
				J.add(usernametxt);
				usernametxt.setEditable(false);
				usernametxt.setBounds(100,280,200,30);
				
				J.add(submitbtn);
				submitbtn.setBounds(100,330,200,60);
	
				
				DataBaseTablee.setModel(new javax.swing.table.DefaultTableModel(
				new Object [][] { {null},{null},{null},{null}},new String [] { ""}) 
				
				{
				Class[] types = new Class [] { java.lang.Object.class};
				boolean[]canEdit=new boolean[]{false};
				
				public Class getColumnClass(int columnIndex) {
				return types [columnIndex];
				}
				
				public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit [columnIndex];
				}
				});
				
				DataBaseTablee.addMouseListener(new java.awt.event.MouseAdapter() {
				    public void mouseReleased(java.awt.event.MouseEvent evt) {
				        DataBaseTableMouseReleasedd(evt);
				    }
				});
				
				DataBaseTablee.setModel(new MyTableModell());
				DataBaseTablee.setVisible(true);
					
				jScrollPane1.setViewportView(DataBaseTablee);
				
				J.add(jScrollPane1);
				jScrollPane1.setBounds(3, 120, 550, 150);
				
				//J.add(k(), BorderLayout.SOUTH);
				
				J.revalidate();
				
				
			 	}return(J);
				}
			
			
				public void DataBaseTableMouseReleasedd(java.awt.event.MouseEvent evt) {
				usernametxt.setText(new MyTableModell().getValueAt(DataBaseTablee.getSelectedRow(),0).toString());
				//System.out.println("Hi");
//				statuslbl.setText("");
//				statuslbl.setBackground(null);
				}
				
//				public JPanel seee () {
//					JPanel j=new JPanel();
//					JLabel lb=new JLabel(adminacc[DataBaseTablee.getSelectedRow()][5]);
//					j.add(lb);
//					JLabel email=new JLabel(adminacc[DataBaseTablee.getSelectedRow()][0]);
//					j.add(email);
//					JLabel e2=new JLabel(adminacc[DataBaseTablee.getSelectedRow()][1]);
//					j.add(e2);
//					JLabel e3=new JLabel(adminacc[DataBaseTablee.getSelectedRow()][2]);
//					j.add(e3);
//					JLabel e4=new JLabel(adminacc[DataBaseTablee.getSelectedRow()][3]);
//					j.add(e4);
//					return(j);
//					
//					
//					}

				class MyTableModell extends AbstractTableModel {
				//You can choose any long value for serialVersionUID.
				//I chose 317 because this is CSCI 317.
					
				String[] s={"Username", "Personal Email", "Name","Address", "Phone", "AccountStatus"};
				private static final long serialVersionUID=317L;
				public int getColumnCount() {
				return (NumberOfColumns);
				}
				public int getRowCount() {
				return(NumberOfRows);
				}
				public String getColumnName(int i) {
				return (s[i]);
				}
				public java.lang.Object getValueAt(int row, int column) {
				return(MyArray[row][column]);
				}
//				public Class getColumnClass(int c) {
//				return getValueAt(0, c).getClass();
//				}
				public boolean isCellEditable(int row, int col) {
				if (col < 6) { 
				return false;
				} else {
				return true;
				}
				}
				public void setValueAt(Object value, int row, int col) {
				MyArray[row][col] = value;
				fireTableCellUpdated(row, col);
				}    				
				}
				
			
}
